package com.ruoyi.fztz.lstb.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.fztz.lstb.domain.Lstb;
import com.ruoyi.fztz.lstb.service.ILstbService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 流水填报Controller
 *
 * @author huacai
 * @date 2024-11-18
 */
@RestController
@RequestMapping("/fztz/lstb")
public class LstbController extends BaseController {
    @Resource
    private ILstbService lstbService;

    /**
     * 查询流水填报列表
     */
    @PreAuthorize("@ss.hasPermi('fztz:lstb:list')")
    @GetMapping("/list")
    public TableDataInfo list(Lstb lstb) {
        startPage();
        List<Lstb> list = lstbService.selectLstbList(lstb);
        return getDataTable(list);
    }

    /**
     * 导出流水填报列表
     */
    @PreAuthorize("@ss.hasPermi('fztz:lstb:export')")
    @Log(title = "流水填报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Lstb lstb) {
        List<Lstb> list = lstbService.selectLstbList(lstb);
        ExcelUtil<Lstb> util = new ExcelUtil<Lstb>(Lstb.class);
        util.exportExcel(response, list, "流水填报数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Lstb> util = new ExcelUtil<Lstb>(Lstb.class);
        util.importTemplateExcel(response, "流水填报数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "流水填报", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('fztz:lstb:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Lstb> util = new ExcelUtil<Lstb>(Lstb.class);
        InputStream inputStream = file.getInputStream();
        List<Lstb> list = util.importExcel(inputStream);
        inputStream.close();
        int count = lstbService.batchInsertLstb(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取流水填报详细信息
     */
    @PreAuthorize("@ss.hasPermi('fztz:lstb:query')")
    @GetMapping(value = "/{lstbId}")
    public AjaxResult getInfo(@PathVariable("lstbId") String lstbId) {
        return success(lstbService.selectLstbByLstbId(lstbId));
    }

    /**
     * 新增流水填报
     */
    @PreAuthorize("@ss.hasPermi('fztz:lstb:add')")
    @Log(title = "流水填报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Lstb lstb) {
        lstb.setUserId(Math.toIntExact(getUserId())); //用户ID
        lstb.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        lstb.setCreateBy(getUsername()); //创建人
        lstb.setIdWhere("存在");
        return toAjax(lstbService.insertLstb(lstb));
    }

    /**
     * 修改流水填报
     */
    @PreAuthorize("@ss.hasPermi('fztz:lstb:edit')")
    @Log(title = "流水填报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Lstb lstb) {
        return toAjax(lstbService.updateLstb(lstb));
    }

    /**
     * 删除流水填报
     */
    @PreAuthorize("@ss.hasPermi('fztz:lstb:remove')")
    @Log(title = "流水填报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{lstbIds}")
    public AjaxResult remove(@PathVariable String[] lstbIds) {
        return toAjax(lstbService.deleteLstbByLstbIds(lstbIds));
    }

    /**
     * 获得流水填报主键ID (UUID)
     */
    @GetMapping("/getLsTbId")
    public AjaxResult getLsTbId(){
        String getLsTbId = IdUtils.fastSimpleUUID();
        return success(getLsTbId);
    }
}
